package com.huang.tiger.utils;

import org.springframework.beans.factory.annotation.Value;

import java.util.ArrayList;
import java.util.List;

/**
 * 加密工具类
 */
public class EncryptionUtils {


    /**
     * 盐
     */
    @Value("${encryption.originalSalt}")
    private String originalSalt;

    private static final String TEL = "16569854125";
    private static final String [] CAPITAL = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
    private static final String [] LOWERCASE = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
    private static final String [] NUMBER = {"0","1","2","3","4","5","6","7","8","9"};
    /**
     * 获取用户盐 ，通过电话号码，用户名生成
     * @param tel
     * @return
     */
    public static String selectSalt(String tel){
        if(tel.length() < 11){
            Integer sub11 = 11-tel.length();
            for(Integer a= 0;a<sub11;a++){
                tel+=NUMBER[a];
            }
        }
        if(tel.length() > 11){
            tel = tel.substring(0,12);
        }
        char[] telArray = tel.toCharArray();
        String salt = "";
        for(Character character:telArray){
            Integer randomInt = RandomUtils.randomInt(17);
            Integer telCharInt = Integer.parseInt(character + "");
            if((randomInt+telCharInt) >= CAPITAL.length){
                salt += CAPITAL[CAPITAL.length];
            }else{
                salt += CAPITAL[randomInt+telCharInt];
            }
        }
        tel = StringUtils.reverse(tel);
        telArray = tel.toCharArray();
        for(Character character:telArray){
            Integer randomInt = RandomUtils.randomInt(17);
            Integer telCharInt = Integer.parseInt(character + "");
            if((randomInt+telCharInt) >= LOWERCASE.length){
                salt += LOWERCASE[LOWERCASE.length];
            }else{
                salt += LOWERCASE[randomInt+telCharInt];
            }
        }
        for(Character character:telArray){
            Integer telCharInt = Integer.parseInt(character + "");
            salt += NUMBER[telCharInt];
        }
        salt = RandomUtils.disrupt(salt).substring(0,20);
        return salt;
    }

    /**
     * 加密
     * @param password
     * @param salt
     * @return
     */
    public static String encryption(String password,String salt){
        if(password.length() < 20){
            Integer sub20 = 20-password.length();
            for(Integer a= 0;a<sub20;a++){
                password+=LOWERCASE[a];
            }
        }
        String one = password.substring(0, 3);
        String two = password.substring(3, 6);
        String three = password.substring(6, 9);
        String four = password.substring(9, 12);
        String five = password.substring(12, 15);
        String six = password.substring(15, 17);
        String seven = password.substring(17, 20);
        password = "";
        password = six + three +five + two +seven + one +four;
        List<String> encryptionList = new ArrayList<>();
        char[] saltCharArray = salt.toCharArray();
        char[] passwordCharArray = password.toCharArray();
        List<String> saltCharList = new ArrayList<>();
        for(Character saltChar:saltCharArray){
            saltCharList.add(saltChar+"");
        }

        for(Integer a= 0;a<saltCharList.size();a++){
            encryptionList.add(saltCharList.get(a));
            if(a <passwordCharArray.length){
                encryptionList.add(passwordCharArray[a]+"");
            }
        }
        String encryptionPassword = "";
        for(String str:encryptionList){
            encryptionPassword += str;
        }
        return encryptionPassword;
    }




}
